package com.gitee.lw114514.test_spring_1;

import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Enumeration;

@RestController
public class RequestController {
    @RequestMapping("/request")
    public String request(HttpServletRequest user_request){
        // 1.获取请求的方法
        String method = user_request.getMethod();
        System.out.println(method);

        // 2. 获取请求的URL
        String url = user_request.getRequestURL().toString();
        System.out.println(url);

        // 3. 获取请求的协议
        String protocol = user_request.getProtocol();
        System.out.println(protocol);

        // 4. 获取请求的参数
        Enumeration<String> parameterNames = user_request.getParameterNames();
        while (parameterNames.hasMoreElements()) {
            String parameterName = parameterNames.nextElement();
            String parameterValue = user_request.getParameter(parameterName);
            System.out.println(parameterName + ":" + parameterValue);
        }

        // 5. 获取请求头
        Enumeration<String> headerNames = user_request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String headerName = headerNames.nextElement();
            String headerValue = user_request.getHeader(headerName);
            System.out.println(headerName + ":" + headerValue);
        }

        String back = "请求方法:" + method + "\n" +
                "请求URL:" + url + "\n" +
                "请求协议:" + protocol + "\n" +
                "请求参数:" + parameterNames + "\n" +
                "请求头:" + headerNames;

        return back;
    }
}
